<template>
  <div class="dashboard-container">
    <el-row :gutter="20">
      <el-col :span="6" v-for="(item, index) in statistics" :key="index">
        <el-card shadow="hover" class="statistics-card">
          <div class="statistics-item">
            <el-icon :size="40" :class="item.color">
              <component :is="item.icon" />
            </el-icon>
            <div class="statistics-info">
              <div class="statistics-value">{{ item.value }}</div>
              <div class="statistics-label">{{ item.label }}</div>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <el-row :gutter="20" class="chart-row">
      <el-col :span="12">
        <el-card shadow="hover">
          <template #header>
            <div class="card-header">
              <span>预约趋势</span>
            </div>
          </template>
          <div class="chart" ref="appointmentChartRef"></div>
        </el-card>
      </el-col>
      <el-col :span="12">
        <el-card shadow="hover">
          <template #header>
            <div class="card-header">
              <span>考试通过率</span>
            </div>
          </template>
          <div class="chart" ref="passRateChartRef"></div>
        </el-card>
      </el-col>
    </el-row>

    <el-row :gutter="20" class="table-row">
      <el-col :span="24">
        <el-card shadow="hover">
          <template #header>
            <div class="card-header">
              <span>最近预约</span>
            </div>
          </template>
          <el-table :data="recentAppointments" style="width: 100%">
            <el-table-column prop="studentName" label="学员姓名" />
            <el-table-column prop="coachName" label="教练姓名" />
            <el-table-column prop="subject" label="培训科目" />
            <el-table-column prop="appointmentTime" label="预约时间" />
            <el-table-column prop="status" label="状态">
              <template #default="{ row }">
                <el-tag :type="row.status === '已完成' ? 'success' : 'warning'">
                  {{ row.status }}
                </el-tag>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import * as echarts from 'echarts'
import {
  User,
  UserFilled,
  Calendar,
  Medal
} from '@element-plus/icons-vue'

// 统计数据
const statistics = ref([
  { label: '总学员数', value: 256, icon: 'User', color: 'primary' },
  { label: '总教练数', value: 12, icon: 'UserFilled', color: 'success' },
  { label: '今日预约', value: 35, icon: 'Calendar', color: 'warning' },
  { label: '考试通过', value: 168, icon: 'Medal', color: 'danger' }
])

// 最近预约数据
const recentAppointments = ref([
  {
    studentName: '张三',
    coachName: '李教练',
    subject: '科目二',
    appointmentTime: '2024-02-20 09:00',
    status: '已完成'
  },
  {
    studentName: '李四',
    coachName: '王教练',
    subject: '科目三',
    appointmentTime: '2024-02-20 10:00',
    status: '待完成'
  },
  {
    studentName: '王五',
    coachName: '赵教练',
    subject: '科目二',
    appointmentTime: '2024-02-20 14:00',
    status: '待完成'
  },
  {
    studentName: '赵六',
    coachName: '李教练',
    subject: '科目三',
    appointmentTime: '2024-02-20 15:00',
    status: '待完成'
  }
])

// 图表相关
const appointmentChartRef = ref(null)
const passRateChartRef = ref(null)

onMounted(() => {
  // 预约趋势图表
  const appointmentChart = echarts.init(appointmentChartRef.value)
  appointmentChart.setOption({
    tooltip: {
      trigger: 'axis'
    },
    xAxis: {
      type: 'category',
      data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
    },
    yAxis: {
      type: 'value'
    },
    series: [{
      data: [30, 25, 35, 40, 35, 20, 15],
      type: 'line',
      smooth: true
    }]
  })

  // 考试通过率图表
  const passRateChart = echarts.init(passRateChartRef.value)
  passRateChart.setOption({
    tooltip: {
      trigger: 'item'
    },
    legend: {
      orient: 'vertical',
      left: 'left'
    },
    series: [
      {
        type: 'pie',
        radius: '50%',
        data: [
          { value: 80, name: '科目一' },
          { value: 70, name: '科目二' },
          { value: 65, name: '科目三' },
          { value: 90, name: '科目四' }
        ],
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)'
          }
        }
      }
    ]
  })

  // 监听窗口大小变化
  window.addEventListener('resize', () => {
    appointmentChart.resize()
    passRateChart.resize()
  })
})
</script>

<style scoped>
.dashboard-container {
  padding: 20px;
}

.statistics-card {
  margin-bottom: 20px;
}

.statistics-item {
  display: flex;
  align-items: center;
  gap: 20px;
}

.statistics-info {
  display: flex;
  flex-direction: column;
}

.statistics-value {
  font-size: 24px;
  font-weight: bold;
}

.statistics-label {
  font-size: 14px;
  color: #909399;
}

.chart-row {
  margin-bottom: 20px;
}

.chart {
  height: 300px;
}

.primary {
  color: #409eff;
}

.success {
  color: #67c23a;
}

.warning {
  color: #e6a23c;
}

.danger {
  color: #f56c6c;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>